import {
	Config
} from './config.js';

// 校验手机号
var checkMobile = (number) => {
	const regex = /^1[0|1|2|3|4|5|6|7|8|9][0-9]{9}$/g
	return regex.test(number)
}

// 校验身份证
var checkCard = (number) => {
	const regex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/g
	return regex.test(number)
}

// 验证姓名
var checkName = (number) => {
	const regex = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/g
	return regex.test(number)
}

// 时间转换  年，月，日，周
var formatTime = (time, fmt) => {
	var o = {
		'M+': time.getMonth() + 1, // 月份
		'd+': time.getDate(), // 日
		'h+': time.getHours(), // 小时
		'm+': time.getMinutes(), // 分
		's+': time.getSeconds(), // 秒
		'q+': Math.floor((time.getMonth() + 3) / 3), // 季度
		'S': time.getMilliseconds() // 毫秒
	}
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (time.getFullYear() + '').substr(4 - RegExp.$1.length))

	var week = {
		'0': '日',
		'1': '一',
		'2': '二',
		'3': '三',
		'4': '四',
		'5': '五',
		'6': '六'
	}
	if (/(E+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? '周' : '') + week[time.getDay() + ''])
	}
	for (var k in o) {
		if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((
			'00' + o[k]).substr(('' + o[k]).length)))
	}
	return fmt
}

// 由经纬度计算两点之间的距离，la为latitude缩写，lo为longitude
// 返回距离单位千米或公里
var distance = (la1, lo1, la2, lo2) => {
	var La1 = la1 * Math.PI / 180.0;
	var La2 = la2 * Math.PI / 180.0;
	var La3 = La1 - La2;
	var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math
		.sin(Lb3 / 2), 2)));
	s = s * 6378.137;
	s = Math.round(s * 10000) / 10000;
	s = s.toFixed(2);
	return s;
}

// 登录，注册后的页面跳转
// 注册后跳转到会员中心
// 登录后跳转到浏览器前的页面，或者是会员中心
var Onpageurl = () => {
	let goUrl = '';
	let returnurl = uni.getStorageSync('returnurl');
	if (returnurl) {
		uni.setStorage({
			key: 'firstObj',
			data: returnurl
		})
		goUrl = returnurl.fullPath
	} else {
		if (uni.getStorageSync('token')) {
			goUrl = '/pages/my/index'
		} else {
			goUrl = '/pages/common/login'
		}
	}

	let barUrl = [
		'/pages/home/index',
		'/pages/hotel/orderList',
		'/pages/my/index'
	]

	if (IsInArray(barUrl, goUrl)) {
		uni.switchTab({
			url: goUrl
		})
	} else {
		uni.navigateTo({
			url: goUrl
		})
	}

	uni.removeStorageSync('returnurl');
}

//微信获取签名参数处理
var OnWxoptions = (res) => {
	let scan = decodeURIComponent(res);
	let options = scan.split(",")
	let obj = {}
	options.forEach(item => {
		let key = item.split('.')[0];
		let data = item.split('.')[1];
		obj[key] = data
	})
	return obj
}


//减法
function numSub(arg1, arg2) {
	let num = 0
	let a1 = parseFloat(arg1) * 1000
	let a2 = parseFloat(arg2) * 1000
	num = a1 - a2
	num = num / 10
	num = parseFloat(num)
	num = Math.floor(num) / 100;
	return num
}

//加法
function numAdd(arg1, arg2) {
	let num = 0
	let a1 = parseFloat(arg1) * 1000
	let a2 = parseFloat(arg2) * 1000
	num = a1 + a2
	num = num / 10
	num = parseFloat(num)
	num = Math.floor(num) / 100;
	return num
}

//乘法
function numMulti(arg1, arg2) {
	let num = 0
	let a1 = parseFloat(arg1) * 1000
	let a2 = parseFloat(arg2) * 1000
	num = a1 * a2
	num = num / 10000
	num = parseFloat(num)
	num = Math.floor(num) / 100;
	return num
}

//判断券的领取量
function Oncoupon(max_number, day_number, count, day_count) {
	let obj = {
		title: '', //按钮文字
		msg: '', //领取提示
		type: null, //领取状态
	}
	//全部领取完毕
	if (max_number <= count) {
		obj = {
			title: '已领完',
			msg: '您已领取完了~',
			type: false,
		}
	} else if (day_number <= day_count && max_number > count) {
		//当日领取完毕，明日可以继续领取
		obj = {
			title: '今日已领完',
			msg: '您今日已领取完毕,请明日在来领取~',
			type: false,
		}
	} else if (day_count == 0 && count == 0) {
		//一次都没有领取过
		obj = {
			title: '立即领取',
			msg: '',
			type: true,
		}
	} else if (max_number > count && day_number > day_count) {
		//可以继续领取
		obj = {
			title: '继续领取',
			msg: '',
			type: true,
		}
	}
	return obj
}


// h5复制
var h5Copy = (content) => {
	if (!document.queryCommandSupported('copy')) {
		// 不支持
		return false
	}

	let textarea = document.createElement("textarea")
	textarea.value = content
	textarea.readOnly = "readOnly"
	document.body.appendChild(textarea)
	textarea.select() // 选择对象
	textarea.setSelectionRange(0, content.length) //核心
	let result = document.execCommand("copy") // 执行浏览器复制命令
	textarea.remove()
	return result
}

// 某变量是否为某数组中的一个值
var IsInArray = (arr, val) => {
	var testStr = ',' + arr.join(",") + ",";
	return testStr.indexOf("," + val + ",") != -1;
}

// 4种跳转方式：1-内部链接，2-外部链接，3-跳转小程序，4-无跳转
var goUrl = (type, url, appid) => {
	let barUrl = [
		'/pages/home/index',
		'/pages/hotel/orderList',
		'/pages/my/index'
	]
	if (type == 1) {
		// 内部链接
		if (IsInArray(barUrl, url)) {
			uni.switchTab({
				url: url
			})
		} else {
			uni.navigateTo({
				url: url
			})
		}
	} else if (type == 2) {
		// 外部链接
		uni.navigateTo({
			url: '/pages/common/out?url=' + url
		})
	} else if (type == 3) {
		// 跳转小程序
		uni.navigateToMiniProgram({
			appId: appid,
			path: url
		})
	} else if (type == 4) {
		// 无跳转
		return false
		// uni.showModal({
		// 	title: '提示',
		// 	confirmColor: '#03004c',
		// 	content: '暂未开放，敬请期待~',
		// 	success: function(res) {
		// 		if (res.confirm) {
		// 			console.log('用户点击确定')
		// 		} else if (res.cancel) {
		// 			console.log('用户点击取消')
		// 		}
		// 	}
		// });
	}
}

export default {
	checkMobile,
	checkCard,
	checkName,
	formatTime,
	distance,
	Onpageurl,
	OnWxoptions,
	numSub,
	numAdd,
	numMulti,
	Oncoupon,
	h5Copy,
	IsInArray,
	goUrl
}
